/**
 * @author wly
 * @version V1.0.0
 * @date 2023/3/17 21:45
 * 非递归实现二分查找法
 */
public class BinarySearch_2 {
    public static <E extends Comparable<E>> int search(E[] data, E target){
        int l = 0, r = data.length - 1;
        while (l <= r){
            int mid = l + (r - l) / 2;
            if (data[mid].compareTo(target) == 0)
                return mid;
            if (data[mid].compareTo(target) < 0)
                l = mid + 1;
            else
                r = mid - 1;
        }
        return -1;
    }

    public static void main(String[] args) {
        Integer[] nums = {-1, 0, 3, 5, 9, 12};
        System.out.println(search(nums, 2));
    }
}
